package com.mikepenz.app.activity.until;

import android.net.Uri;
import android.support.v4.view.ViewPager;

import com.mikepenz.app.R;
import com.mikepenz.app.adapter.MPagerAdapter;
import com.mikepenz.app.fragment.OrderManagerFragment;
import com.mikepenz.app.fragment.OrderManagerFragment2;
import com.quickdv.activity.fragment.BaseFragment;
import com.viewpagerindicator.TabPageIndicator;

import java.util.ArrayList;
import java.util.List;

import it.neokree.materialtabs.MaterialTab;
import it.neokree.materialtabs.MaterialTabHost;
import it.neokree.materialtabs.MaterialTabListener;

/***
 *
 * 订单列表
 */

public class PageIndicatorActivity extends com.quickdv.activity.BaseActivity implements BaseFragment.OnFragmentInteractionListener, MaterialTabListener {
    private MPagerAdapter adapter;
    private ViewPager vp_order;
    private TabPageIndicator ct_titles;
    private List<BaseFragment> data;
    private MaterialTabHost tabHost;


    @Override
    protected void onDestroy() {
        super.onDestroy();
        adapter = null;
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_order;
    }

    @Override
    public void initView() {

        vp_order = (ViewPager) findViewById(R.id.order_vp);
        tabHost = (MaterialTabHost) this.findViewById(R.id.materialTabHost);
        buildData(title, 0);
        binder(vp_order, null);

    }

    String [] title = new String[]{"已还车订单","当前使用订单"};


    protected void buildData(String[] title,int type){

        data = new ArrayList<>();
        OrderManagerFragment frag = OrderManagerFragment.newInstance(0);
        data.add(frag);
        OrderManagerFragment2 frag2 = OrderManagerFragment2.newInstance(1);
        data.add(frag2);
    }

    public void binder(ViewPager vp, TabPageIndicator tab) {
        buildData(title, Status.PageType.HLEP.getType());
        MPagerAdapter adapter = new MPagerAdapter(getSupportFragmentManager(),data,title);
        vp.setAdapter(adapter);
        vp.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                // when user do a swipe the selected tab change
                tabHost.setSelectedNavigationItem(position);
            }
        });

        // insert all tabs from pagerAdapter data
        for (int i = 0; i < adapter.getCount(); i++) {
            tabHost.addTab(
                    tabHost.newTab().setText(title[i])
//                            .setIcon(getIcon(i))
                            .setTabListener(PageIndicatorActivity.this)
            );
        }
    }

    @Override
    public void initActionbar() {
        getSupportActionBar().setTitle("订单列表");
    }

    @Override
    public void initData() {

    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    @Override
    public void onTabSelected(MaterialTab tab) {
        // when the tab is clicked the pager swipe content to the tab position
        vp_order.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabReselected(MaterialTab materialTab) {

    }

    @Override
    public void onTabUnselected(MaterialTab materialTab) {

    }
}
